/**
 * 
 */
package com.zito.common;

import java.io.File;
import java.io.IOException;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.name.Rename;

import com.downlove.common.RequestContext;

/**
 * @author YunHai.Jiang
 * 
 */
public class ImagesHandler {
	private static String newFileSuffix = "png";
	private static String ThumbFilePath = "";

	public ImagesHandler Thumbnail(String FilePath) {
		File originalImage = new File(RequestContext.root()+FilePath);
		//System.out.println(originalImage);
		int indexOfDot = FilePath.lastIndexOf('.');
		if (indexOfDot != -1) {
			newFileSuffix = FilePath.substring(indexOfDot + 1, FilePath
					.length());
			if ("bmp".equals(newFileSuffix.toLowerCase())) {
				newFileSuffix = "png";
			}
		}
		//System.out.println("newFileSuffix : "+ newFileSuffix);
		
		try {
			Thumbnails.of(originalImage).size(
					RequestContext.get().config().getInt("album.thumbnail.width",
							100),
					RequestContext.get().config().getInt("album.thumbnail.height",
							100)).outputFormat(newFileSuffix).toFiles(
					SUFFIX_DOT_THUMBNAIL);
		} catch (IOException e) {
			e.printStackTrace();
		}
		ThumbFilePath = mySuffix(FilePath, RequestContext.get().config()
				.getString("album.thumbnail.suffix", "_thumb"));
		//System.out.println("ThumbFilePath : "+ ThumbFilePath);
		return this;
	}

	/**
	 * 获得新的图片名称
	 */
	private static final Rename SUFFIX_DOT_THUMBNAIL = new Rename() {
		@Override
		public String apply(String fileName) {
			// return mySuffix(fileName, "_thumb");
			return mySuffix(fileName, RequestContext.get().config().getString(
					"album.thumbnail.suffix", "_thumb"));
		}
	};

	/**
	 * 获得新的图片名称
	 * 
	 * @param fileName
	 * @param suffix
	 * @return
	 */
	private static String mySuffix(String fileName, String suffix) {
		String newFileName = "";
		int indexOfDot = fileName.lastIndexOf('.');
		if (indexOfDot != -1) {
			newFileName = fileName.substring(0, indexOfDot);
			newFileName += suffix;
		} else {
			newFileName = fileName + suffix;
		}
		return newFileName;
	}

	/**
	 * 图片以及生成缩略图的路径和名称
	 * 
	 * @return 获得ImageInfo类对象
	 */
	public ThumbnailInfo getThumbnailInfo() {
		return new ThumbnailInfo();
	}

	public class ThumbnailInfo {
		private String imageThumbPath = "";
		private String imageThumbName = "";

		public String getImageThumbPath() {
			return imageThumbPath;
		}

		public ThumbnailInfo() {
			this.imageThumbPath = ThumbFilePath + "." + newFileSuffix;
			int indexOfDotT = this.imageThumbPath.lastIndexOf(File.separator
					.replace("\\", "/"));
			if (indexOfDotT != -1) {
				this.imageThumbName = this.imageThumbPath.substring(
						indexOfDotT + 1, this.imageThumbPath.length());
			} else {
				this.imageThumbName = this.imageThumbPath;
			}

		}

		public void setImageThumbPath(String imageThumbPath) {
			this.imageThumbPath = imageThumbPath;
		}

		public String getImageThumbName() {
			return imageThumbName;
		}

		public void setImageThumbName(String imageThumbName) {
			this.imageThumbName = imageThumbName;
		}

	}

}
